import { vector3Offset } from "../core/common.js";
import global from "../core/global.js";
import { changeShadingModel } from "../core/scene.js";
let Tiles = {
  add(options, cb) {
    let scene = window.viewer.scene;
    // let aliUrl = 'https://zgy-shushengcs.oss-cn-shenzhen.aliyuncs.com'
    // if (options.url.includes(aliUrl)) {
    //   options.url = options.url.replace(aliUrl, 'https://www.dataarche.com')
    // }
    let tiles = new SSmap.Tileset(options.url);
    if (options.headerList && options.headerList.length) {
      options.headerList.map((item) => {
        tiles.setHeader(item.name, item.value);
      });
    }
    let entity = new SSmap.Entity();
    entity.addComponent(tiles);
    scene.addEntity(entity);
    if (options.id) {
      global.tilesList.add(tiles, options.id);
    } else {
      options.id = global.tilesList.add(tiles);
    }
    this.transform(options, cb);
    entity.tag = options.id;
    tiles.entity.enabled = options.show || true;
    tiles.debugBoundingVolume = options.debugBoundingVolume || false; //显示包围盒
    tiles.geometryError = options.geometryError || 3; //消失时机[0,12](像素)0就是不消失
    tiles.skipLevelOfRenderable = options.skipLevelOfRenderable != undefined ? options.skipLevelOfRenderable : 4;
    tiles.geometricErrorScale = options.geometricErrorScale != undefined ? options.geometricErrorScale : 10;
    tiles.maximumMemoryUsage = options.maximumMemoryUsage != undefined ? options.maximumMemoryUsage : 4090;
    tiles.compatible = true;
    tiles.cullRequestsWhileMoving = false;
    tiles.cullRequestsWhileMovingMultiplier = 60;
  },
  transform(options, cb) {
    let tiles = global.tilesList.get(options.id);
    let rotation = options.rotation;
    let offset = options.offset;
    let _rotation = SSmap.Quaternion.fromEulerAngles(
      (rotation && rotation.x) || 0,
      (rotation && rotation.y) || 0,
      (rotation && rotation.z) || 0
    ); //四元数 控制旋转
    let _offset = SSmap.Vector3.create(
      (offset && offset.x) || 0,
      (offset && offset.y) || 0,
      (offset && offset.z) || 0
    ); //偏移量
    let matrix3 = _rotation.toRotationMatrix();
    tiles.modelMatrix = SSmap.Matrix4.fromRotationTranslation(matrix3, _offset);
    if (typeof cb == "function" || options.location) {
      if (tiles.readyPromise) {
        tiles.readyPromise.then(() => {
          if (options.unlit) changeShadingModel(tiles);
          options.location && Tiles.flyTo(options);
          cb && cb(options.id);
        });
      } else {
        cb && cb(options.id);
      }
    }
  },
  flyTo(options, cb = () => { }) {
    let tiles = global.tilesList.get(options.id);
    let scene = window.viewer.scene;
    let camera = scene.mainCamera;
    let cameraController = camera.cameraController();
    let boundingVolume = this._getBoundingVolume(options);
    let center = boundingVolume.center;
    let radius = boundingVolume.radius;
    let carto = center.toCartographic();
    let height = carto.height;
    carto.height += radius * 1.5;
    carto.height = Math.abs(carto.height + radius);
    let position = carto.toVector3();
    /* position = vector3Offset(position, {
      offsetY: -(radius * 1.5)
    }) */
    cameraController.flyTo(position, 2, 0, -90, 0);
    cb(true);
  },
  show(options) {
    let tiles = global.tilesList.get(options.id);
    tiles.entity.enabled = options.show || false;
  },

  remove(options) {
    global.tilesList.remove(options.id, true);
  },
  _getBoundingVolume(options) {
    let tiles = global.tilesList.get(options.id);
    let boundingVolume = tiles.rectangle.bounds();
    return boundingVolume;
  },
  _getTiles(options) {
    let tiles = global.tilesList.get(options.id);
    return tiles;
  },
  isShow(id, cb) {
    let tiles = global.tilesList.get(id);
    const show = tiles ? tiles.enabled : false;
    cb(show);
  },
};

export { Tiles };
